﻿Imports VendasDAL
Imports System.Text.RegularExpressions
Public Class FuncionarioBLL
    Dim ObjDAL As New FuncionarioDAL
#Region "Propriedades"
    Property Codigo As Integer
    Property Nome As String
    Property Endereco As String
    Property Num As String
    Property Bairro As String
    Property Cidade As String
    Property Uf As String
    Property Complemento As String
    Property Cep As String
    Property Email As String
    Property Obs As String
    Property DtNascimento As Date
    Property Status As Integer
    Property ccc_codigo As Integer
    Property cpf As String
    Property rg As String
    Property telres As String
    Property telcom As String
    Property telcel As String
    Property telfax As String
    Property numcarteira As String
    Property DtAdmissao As Date
    Property DtDemissao As Date = Date.Now
    Property CodCargo As Integer
    Property Cardescricao As String
    Property Salario As Decimal
#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(codigo As Integer, nome As String, endereco As String, num As String, bairro As String, cidade As String, uf As String, complemento As String, cep As String,
            email As String, obs As String, dtnascimento As Date, status As Integer, cpf As String, rg As String,
            telres As String, telcel As String, telfax As String, numcarteira As String, dtadmissao As Date, dtdemissao As Date, codcargo As Integer, salario As Decimal)
        Me.Codigo = codigo
        Me.Nome = nome
        Me.Endereco = endereco
        Me.Num = num
        Me.Bairro = bairro
        Me.Cidade = cidade
        Me.Uf = uf
        Me.Complemento = complemento
        Me.Cep = cep
        Me.Email = email
        Me.Obs = obs
        Me.DtNascimento = dtnascimento
        Me.Status = status
        Me.cpf = cpf
        Me.rg = rg
        Me.telres = telres
        Me.telcom = telcom
        Me.telcel = telcel
        Me.telfax = telfax
        Me.numcarteira = numcarteira
        Me.DtAdmissao = dtadmissao
        Me.DtDemissao = dtdemissao
        Me.CodCargo = codcargo
        Me.Salario = salario
    End Sub
    Sub New(codigo)
        carrega(codigo)
    End Sub
#End Region

#Region "Inserir/alterar cliente"
    Public Function InserirAlterar()
        Try

            If (Me.Nome <> "" And Me.Endereco <> "" And Me.Bairro <> "" And Me.Cidade <> "" And Me.Num <> "" And Me.Uf <> "" And Me.Cep <> "" And Me.CodCargo > 0 And Me.Salario > 0 And numcarteira <> "") Then
                If (Me.Cep.Length = 8) Then
                    If (ValidaCPF()) Then
                        If (ValidaEmail(Me.Email)) Then
                            If (validar().Rows.Count = 0) Then
                                ObjDAL = New FuncionarioDAL
                                If (ObjDAL.ValidaExcluir(Me.Codigo, Me.Status)) Then
                                    Me.Codigo = ObjDAL.InserirAlterar(Me.Codigo, Me.Nome, Me.Endereco, Me.Num, Me.Bairro, Me.Cidade, Me.Uf, Me.Complemento, Me.Cep, Me.Email, Me.Obs, Me.DtNascimento, Me.Status, Me.cpf, Me.rg, Me.telres, Me.telcom, Me.telcel, Me.telfax, Me.numcarteira, Me.DtAdmissao, Me.DtDemissao, Me.CodCargo, Me.Salario)
                                Else : Throw New Exception("Este funcionário não pode ser modificado, verifique antes a ocorrência do mesmo no(s) formulário(s): Usuários")
                                End If
                            Else : Throw New Exception("Funcionário já cadastrado")
                            End If
                        Else : Throw New Exception("Email inválido")
                        End If
                    Else : Throw New Exception("CPF inválido")
                    End If
                Else : Throw New Exception("CEP inválido")
                End If
            Else : Throw New Exception("Favor preencher os campos obrigatórios")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (Me.Codigo <= 0) Then
            Return False
        Else : Return True
        End If
    End Function
#End Region

#Region "Validações"
    Public Function ValidaCPF() As Boolean
        Dim i, a, n1, n2 As Integer
        cpf = Me.cpf

        If cpf = "" OrElse _
          cpf.Trim.Length <> 11 OrElse _
          cpf = "11111111111" OrElse _
          cpf = "22222222222" OrElse _
          cpf = "33333333333" OrElse _
          cpf = "44444444444" OrElse _
          cpf = "55555555555" OrElse _
          cpf = "66666666666" OrElse _
          cpf = "77777777777" OrElse _
          cpf = "88888888888" OrElse _
          cpf = "99999999999" Then
            Return False
        End If

        For a = 0 To 1
            n1 = 0
            For i = 1 To 9 + a
                n1 = n1 + Val(Mid(cpf, i, 1)) * (11 + a - i)
            Next
            n2 = 11 - (n1 - (Int(n1 / 11) * 11))
            If n2 = 10 Or n2 = 11 Then n2 = 0
            If n2 <> Val(Mid(cpf, 10 + a, 1)) Then
                Return False
            End If
        Next
        Return True

    End Function

    Function ValidaEmail(ByVal email As String) As Boolean
        If email = "" Then
            Return True
        Else
            Dim strRegex As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" + "\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" + ".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
            Dim re As New Regex(strRegex)
            If (re.IsMatch(email)) Then
                Return True
            Else
                Return False
            End If
        End If
    End Function
    
#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            ObjDAL = New FuncionarioDAL
            ret = ObjDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function

#End Region

#Region "Validar por replica"
    Private Function validar() As DataTable
        Dim ret As DataTable
        Try
            ret = objfuncoes.ValidaReplica(Me.cpf, Me.Codigo, "Vendas.Ofi_Funcionario", "fun_codigo", "fun_cpf")
        Catch ex As Exception
            Throw ex
        Finally

        End Try
        Return ret
    End Function
#End Region

#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            ObjDAL = New FuncionarioDAL
            tab = ObjDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.Codigo = Integer.Parse(tab.Rows(0)("fun_codigo").ToString)
                Me.Nome = tab.Rows(0)("fun_nome").ToString
                Me.Endereco = tab.Rows(0)("fun_end").ToString
                Me.Num = tab.Rows(0)("fun_numero").ToString
                Me.Bairro = tab.Rows(0)("fun_bairro").ToString
                Me.Cidade = tab.Rows(0)("fun_cidade").ToString
                Me.Uf = tab.Rows(0)("est_uf").ToString
                Me.Complemento = tab.Rows(0)("fun_complemento").ToString
                Me.Cep = tab.Rows(0)("fun_cep").ToString
                Me.Email = tab.Rows(0)("fun_email").ToString
                Me.Obs = tab.Rows(0)("fun_obs").ToString
                Me.DtNascimento = tab.Rows(0)("fun_dtnasc")
                '  Me.DtCadastro = tab.Rows(0)("cli_dtcadastro")
                Me.Status = Val(tab.Rows(0)("sta_codigo"))
                ' Me.ccc_codigo = (tab.Rows(0)("ccc_codigo"))
                Me.cpf = (tab.Rows(0)("fun_cpf")).ToString
                Me.rg = (tab.Rows(0)("fun_rg")).ToString
                Me.telres = (tab.Rows(0)("fun_telres")).ToString
                ' Me.telcom = (tab.Rows(0)("fun_telcom")).ToString
                Me.telcel = (tab.Rows(0)("fun_telcel")).ToString
                Me.telfax = (tab.Rows(0)("fun_telfax")).ToString
                Me.numcarteira = (tab.Rows(0)("fun_ncarteira")).ToString
                Me.DtAdmissao = (tab.Rows(0)("fun_dtadmissao")).ToString
                If (tab.Rows(0)("fun_dtdemissao").ToString) = "" Then
                Else
                    Me.DtDemissao = tab.Rows(0)("fun_dtdemissao").ToString
                End If

                Me.Cardescricao = (tab.Rows(0)("car_descricao")).ToString
                Me.Salario = (tab.Rows(0)("fun_salario")).ToString
                Me.CodCargo = (tab.Rows(0)("car_codigo")).ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
    End Sub
#End Region

End Class
